// pages/orderCommit/order.js
import {getOrder,getAddress,payment} from "../../api/user.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: 0,
    show:false,//底部地址框
    address:{},//地址信息
    addressAll:[], //所有收货地址
    goodsInfo:[], //订单商品
    totalPrice:0,//订单商品总价格
    totalCount:0,//商品总数量
  },

  // 支付
  async pay(e) {
    let _this = this;
    let order = []
    this.data.goodsInfo.forEach(item=>{
      order.push({
        alias:item.goods_id_alias,
        num:item.goods_num,
        address_id: _this.data.address.address_id,
        price:item.real_price
      })
    })
    order = JSON.stringify(order);
    let data = await payment(order);
    let { nonce_str, timeStamp,prepay_id, paySign, mypackage, sign_type } = data.result.xml;
    wx.requestPayment({
      nonceStr: nonce_str,
      package: mypackage,
      signType: sign_type,
      paySign: paySign,
      timeStamp: timeStamp,
      success: function(res){
        console.log('支付成功',res)
      },
      fail: function(err){
        console.log('支付失败',err)
      },
      complete:(res)=>{
        wx.redirectTo({
          url: '/pages/my_order/order',
        })
      }
    })
  },

  // 点击地址
  clickAddres() {
    this.setData({
      show:true
    })
  },

  // 获取所有地址
  async getAllAddres() {
    let {data} = await getAddress();
    this.setData({
      addressAll:data,
    })
  },

  // 关闭弹出层
  onClose() {
    this.setData({
      show:false
    })
  },

  // 点击选择地址
  addressSelect(e) {
    let {item,index} = e.currentTarget.dataset;
    this.setData({
      address:item,
      radio:index
    })
    this.onClose();
  },

  // 编辑地址
  edit(e) {
    let {item} = e.currentTarget.dataset;
    item = JSON.stringify(item);
    wx.navigateTo({
      url: `/pages/editAddress/edit?myAddres=${item}`,
    })
  },

  // 选择器触发
  onChange(event) {
    // console.log(event.detail);
    this.setData({
      radio: event.detail,
    });
  },

  // 获取确认订单中的数据
  async getOrderInfo(goodsInfos) {
    let {address,goodsInfo,totalPrice,totalCount} = await getOrder(goodsInfos);
    if(address.region) {
      address.region = address.region.split('/').join('');
    }
    this.setData({
      address,goodsInfo,totalPrice,totalCount
    })
  },

  // 跳转添加地址
  addAddress() {
    wx.navigateTo({
      url: '/pages/newAddress/newAddress',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    this.getOrderInfo(options.goodsId);
    // this.getOrderInfo(JSON.stringify([{alias:'2xk8h98efb0k8', num:2},{alias:'3npcouw2wxe7c',num:1}]));
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAllAddres();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})